#--coding:utf-8--
# @Time    : 2022/5/10 16:29
# @Author  : hakold
# @File    : player_model.py
# @PROJECT : chatroom_game
import json
from unicodedata import name
import pymongo

global player_attribute_config
player_attribute_config = json.load(open("config/player_attribute_config.json", encoding='utf-8'))

def mongo_connect():
    #myclient = pymongo.MongoClient('mongodb+srv://hakold:52Beebee@cluster0.gy1rb.mongodb.net/xiuxian?retryWrites=true&w=majority/')
    myclient = pymongo.MongoClient('mongodb://127.0.0.1:27017/')
    #dblist = myclient.list_database_names()
    # dblist = myclient.database_names() 
    mydb = myclient["qt_game"] #创建数据库
    #collist = mydb.list_collection_names()
    mycol = mydb["player"] #集合
    return mycol

def player_attribut_logic(mycol, player_data, effect_type, effect_value):
    # 用于判断修改的属性是什么
    global player_attribute_config
    msg = ""
    if effect_type == "hp":
        # 当前生命值
        if player_data['hp'] + effect_value >= player_data['max_hp']:
            effect_value = player_data['max_hp'] - player_data['hp']
            if effect_value <= 0:
                effect_value = 0
            player_data['hp'] = player_data['max_hp']
        else:
            player_data['hp'] += effect_value
        newvalues = { "$set": { effect_type : player_data['hp'] } }
        mycol.update_one({ "account" : player_data['account'] }, newvalues)
        msg = "属性变化" + "【" + str(effect_value) + "】" +"点"  + "【" + player_attribute_config[effect_type] + "】"
    elif effect_type == "level":
        # 等级突破
        newvalues = { "$set": { effect_type : player_data[effect_type]+effect_value } }
        mycol.update_one({ "account" : player_data['account'] }, newvalues)
        msg = "恭喜你！" + player_attribute_config[effect_type] + "突破了！"
    else:
        newvalues = { "$set": { effect_type : player_data[effect_type]+effect_value } }
        mycol.update_one({ "account" : player_data['account'] }, newvalues)
        msg = "属性变化" + "【" + str(effect_value) + "】" +"点"  + "【" + player_attribute_config[effect_type] + "】"
    return msg
    
def player_attribute_change(self, account, effect_type, effect_value):
    # 玩家属性更换
    # self 父类方法，可以用于调用发送消息的方法
    # account 玩家的用户名(str)
    # effect_type 增加的类型(str)
    # effect_value 本次增加的数值(int) -> 用来发送给玩家
    mycol = mongo_connect()
    # 获取玩家数据
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0] # 玩家数据
    del player_data["_id"]
    msg = player_attribut_logic(mycol, player_data, effect_type, effect_value)
    
    data = {}
    data["type"] = "player_attribute_change"
    data['message'] = msg
    data = json.dumps(data)
    self.data_send_to_user(data, account)

def player_attribute_dict_change(self, account, attr_dict):
    # 玩家装备属性更换
    # self 父类方法，可以用于调用发送消息的方法
    # account 玩家的用户名(str)
    # attr_dict 是减少的属性值
    mycol = mongo_connect()
    # 获取玩家数据
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    player_data = check_list[0] # 玩家数据
    del player_data["_id"]
    
    msg = ""
    for attr_name in attr_dict:
        print(str(attr_name) + ":" + str(player_data[attr_name]) + str(attr_dict[attr_name]))
        player_data[attr_name] += attr_dict[attr_name]
        msg += "属性变化" + "【" + str(attr_dict[attr_name]) + "】" +"点"  + "【" + player_attribute_config[attr_name] + "】" + "\n"

    newvalues = { "$set": 
        { 
            "ad" : player_data['ad'],
            "ap" : player_data['ap'],
            "ad_def" : player_data['ad_def'],
            "ap_def" : player_data['ap_def']
        } 
    }
    mycol.update_one({ "account" : player_data['account'] }, newvalues)

    data = {}
    data["type"] = "player_attribute_change"
    data['message'] = msg
    data = json.dumps(data)
    self.data_send_to_user(data,account)    

